package org.abratuhi.kvngroupware.registration.widget;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.registration.manager.RegistrationManager;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.util.Util;
import org.abratuhi.kvngroupware.widget.AbstractWorkWidget;
import org.jdom.Element;

@SuppressWarnings("serial")
public class GuiRegistration extends AbstractWorkWidget implements ActionListener{
	
	RegistrationManager regmanager;
	
	JLabel labelUserId = new JLabel();
	JLabel labelUserPassword = new JLabel();
	JLabel labelUserName = new JLabel();
	JLabel labelUserSurname = new JLabel();
	JLabel labelUserTeam = new JLabel();
	
	JTextField fieldUserId = new JTextField();
	JTextField fieldUserPassword = new JTextField();
	JTextField fieldUserName = new JTextField();
	JTextField fieldUserSurname = new JTextField();
	JComboBox comboUserTeam = new JComboBox();
	
	JLabel labelTeamName = new JLabel();
	JLabel labelTeamTown = new JLabel();
	JLabel labelTeamCountry = new JLabel();
	
	JTextField fieldTeamName = new JTextField();
	JTextField fieldTeamTown = new JTextField();
	JTextField fieldTeamCountry = new JTextField();
	
	public JButton buttonCreateUser = new JButton();
	public JButton buttonCreateTeam = new JButton();
	
	
	public GuiRegistration(MessageSender sender, Properties locale, RegistrationManager regmanager){
		super("", "KVNGroupware Client: Registration", sender, locale);
		this.regmanager = regmanager;
		
		this.buttonCreateTeam.setText(locale.getProperty("registration.button.team.create"));
		this.buttonCreateUser.setText(locale.getProperty("registration.button.user.create"));
		
		this.labelTeamCountry.setText(locale.getProperty("registration.label.team.country"));
		this.labelTeamName.setText(locale.getProperty("registration.label.team.name"));
		this.labelTeamTown.setText(locale.getProperty("registration.label.team.city"));
		this.labelUserId.setText(locale.getProperty("registration.label.user.id"));
		this.labelUserPassword.setText(locale.getProperty("registration.label.user.password"));
		this.labelUserName.setText(locale.getProperty("registration.label.user.name"));
		this.labelUserSurname.setText(locale.getProperty("registration.label.user.surname"));
		this.labelUserTeam.setText(locale.getProperty("registration.label.user.team"));
		
		this.buttonCreateTeam.addActionListener(this);
		this.buttonCreateUser.addActionListener(this);
		
		JPanel panelCreateUser = new JPanel();
		panelCreateUser.setBorder(new TitledBorder(locale.getProperty("registration.border.newuser")));
		panelCreateUser.setLayout(new SpringLayout());
		panelCreateUser.add(labelUserId);
		panelCreateUser.add(fieldUserId);
		panelCreateUser.add(labelUserPassword);
		panelCreateUser.add(fieldUserPassword);
		panelCreateUser.add(labelUserName);
		panelCreateUser.add(fieldUserName);
		panelCreateUser.add(labelUserSurname);
		panelCreateUser.add(fieldUserSurname);
		panelCreateUser.add(labelUserTeam);
		panelCreateUser.add(comboUserTeam);
		panelCreateUser.add(new JLabel());
		panelCreateUser.add(buttonCreateUser);
		SpringUtilities.makeCompactGrid(panelCreateUser, 6, 2, 0, 0, 0, 0);
		
		JPanel panelCreateTeam = new JPanel();
		panelCreateTeam.setBorder(new TitledBorder(locale.getProperty("registration.border.newteam")));
		panelCreateTeam.setLayout(new SpringLayout());
		panelCreateTeam.add(labelTeamName);
		panelCreateTeam.add(fieldTeamName);
		panelCreateTeam.add(labelTeamTown);
		panelCreateTeam.add(fieldTeamTown);
		panelCreateTeam.add(labelTeamCountry);
		panelCreateTeam.add(fieldTeamCountry);
		panelCreateTeam.add(new JLabel());
		panelCreateTeam.add(buttonCreateTeam);
		SpringUtilities.makeCompactGrid(panelCreateTeam, 4, 2, 0, 0, 0, 0);
		
		this.setLayout(new SpringLayout());
		setPreferredSize(new Dimension(400, 175));
		this.add(panelCreateUser);
		this.add(panelCreateTeam);
		SpringUtilities.makeCompactGrid(this, 1, 2, 0, 0, 0, 0);
		
		this.setVisible(true);
		
	}
	
	public void onFrameClose(){
		deleteWindow();
	}
	
	public void paint(Graphics g){
		//String[] userdata = getNewUserData();
		//String[] teamdata = getNewTeamData();
		super.paint(g);
		String[] teamIds = regmanager.getDataForWidget();
		System.out.println("GuiRegistration: "+Util.asString(teamIds));
		comboUserTeam.setModel(new DefaultComboBoxModel(teamIds));
		//comboUserTeam.removeAllItems();
		//for(int i=0; i<teamIds.length; i++)
		//	comboUserTeam.addItem(teamIds[i]);
		//comboUserTeam.setSelectedIndex(-1);
		//comboUserTeam = new JComboBox(teamIds);
		//fieldUserId.setText(userdata[0]);
		//fieldUserPassword.setText(userdata[1]);
		//fieldUserName.setText(userdata[2]);
		//fieldUserSurname.setText(userdata[3]);
		//comboUserTeam.setSelectedItem(userdata[4]);
		//fieldTeamName.setText(teamdata[0]);
		//fieldTeamTown.setText(teamdata[1]);
		//fieldTeamCountry.setText(teamdata[2]);
	}
	
	public String[] getNewUserData(){
		return new String[]{fieldUserId.getText(), fieldUserPassword.getText(), fieldUserName.getText(), fieldUserSurname.getText(), comboUserTeam.getSelectedItem().toString()};
	}
	
	public String[] getNewTeamData(){
		return new String[]{fieldTeamName.getText(), fieldTeamTown.getText(), fieldTeamCountry.getText()};
	}
	
	public void clearTeamData(){
		fieldTeamName.setText("");
		fieldTeamTown.setText("");
		fieldTeamCountry.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(buttonCreateUser)){
			String[] newUserData = getNewUserData();
			User u = new User(newUserData[0], newUserData[1], newUserData[2], newUserData[3], newUserData[4]);
			
			String[] attrNames = new String[]{"type", "action"};
			String[] attrValues= new String[]{"user", "create"};
			String text = new String();
			Element[] content = new Element[]{u.toElement()};
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
		}
		else if(e.getSource().equals(buttonCreateTeam)){
			String[] newTeamData = getNewTeamData();
			Team t = new Team(newTeamData[0], newTeamData[1], newTeamData[2]);
			
			String[] attrNames = new String[]{"type", "action"};
			String[] attrValues= new String[]{"team", "create"};
			String text = new String();
			Element[] content = new Element[]{t.toElement()};
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
		}
	}
}
